package com.innjia.utils;

import org.apache.commons.lang3.RandomUtils;

/**
 * 生成随机字符串
 */
public class CustomRandomUtils {
    private static final String[] CHARS = {
       "A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z",
       "a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z",
    };

    private static final String[] NUMS = {
        "0","1","2","3","4","5","6","7","8","9"
    };

    /*
     * 返回字母数字组合随机字符串
     * @param minCharLength: 包含最小字符数
     *
     */
    public static String randomStrBothCharNum(int length, int minCharLength){
        if(minCharLength <0 ){
            minCharLength = 0;
        }
        if(minCharLength > length){
            minCharLength = length;
        }
        //获得字符的随机个数
        int charCnt = RandomUtils.nextInt(minCharLength, length + 1);
        int numCnt = length - charCnt;

        return randomNumStr(numCnt) + randomCharStr(charCnt);
    }

    //返回指定个数的随机数字字符串
    public static String randomNumStr(int length){
        if(length < 1){
            return "";
        }

        StringBuilder result = new StringBuilder();
        for(int i = 0; i < length; i++){
            result.append(NUMS[RandomUtils.nextInt(0, NUMS.length)]);
        }

        return result.toString();
    }

    //返回指定个数的随机字母字符串
    public static String randomCharStr(int length){
        if(length < 1){
            return "";
        }

        StringBuilder result = new StringBuilder();
        for(int i = 0; i < length; i++){
            result.append(CHARS[RandomUtils.nextInt(0, CHARS.length)]);
        }

        return result.toString();
    }

    public static void main(String[] args){
        for(int i=0; i<10000; i++){
            System.out.println(randomStrBothCharNum(4, 4));
        }
    }
}
